<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="departmentDto.departmentName" placeholder="学院名称"/>
      </el-form-item>

      <el-form-item>
        <el-input v-model="departmentDto.departmentCode" placeholder="学院代码"/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
      <el-button type="success" plain @click="saveClear">新增学院</el-button>
    </el-form>

    <el-table :data="list" border fit highlight-current-row style="text-align:center">
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">{{ (page - 1) * limit + scope.$index + 1 }}</template>
      </el-table-column>

      <el-table-column prop="departmentName" label="学院名称" align="center"/>

      <el-table-column prop="departmentCode" label="学院代码" align="center"/>

      <el-table-column prop="departmentDes" label="学院描述" align="center"/>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <!-- <router-link :to="'/student/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>-->
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="getDepartmentByCode(scope.row.departmentCode)"
          >修改</el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.departmentCode)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- -------------------------添加或修改学院信息弹框---------------------------------- -->

    <el-dialog title="学院信息" :visible.sync="dialogFormVisible" :append-to-body="true" width="350px">
      <el-form :model="item">
        <el-form-item label="学院名称" :label-width="formLabelWidth">
          <el-input v-model="item.departmentName" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>

        <el-form-item label="学院代码" :label-width="formLabelWidth">
          <el-input v-model="item.departmentCode" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>

        <el-form-item label="学院描述" :label-width="formLabelWidth">
          <el-input v-model="item.departmentDes" autocomplete="off" style="width:300px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdateDepartment">确 定</el-button>
      </div>
    </el-dialog>

    <!-- -------------------------添加或修改学院信息弹框---------------------------------- -->

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
    <!-- 这里  @current-change="getList" 不用传页数，饿了么内部封装了，只需要调方法即可。这个方法加括号不行，分页换显示不了-->
  </div>
</template>


<script>
import org from "@/api/organization/organization.js"; //.js可以省略
export default {
  //2、写核心代码位置
  //data两种写法：
  // data:{
  // },
  data() {
    //定义数据变量和初始值
    return {
      //这里属性名可以自己起
      list: null, //查询之后接口返回集合
      total: 0,
      page: 1, //当前页
      limit: 5, //每页显示记录数
      departmentDto: {}, //条件封装对象
      item: {},
      dialogFormVisible: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList(page = 1) {
      this.page = page;
      org
        .getDepartments(this.page, this.limit, this.departmentDto)
        .then(response => {
          //response接口返回的数据
          // console.log(response)
          //6、打印返回的测试数据
          this.list = response.data.records; //records是后端接口返回的值
          this.total = response.data.total;
          //   console.log(this.list);
          //   console.log(this.total);
        }) //请求成功
        .catch(error => {
          console.log(error);
        }); //请求失败
    },
    resetData() {
      //清空的方法
      //表单输入项数据清空
      this.departmentDto = {};
      //查询度所有的表单数据
      this.getList();
    },

    //新增学院清空表单
    saveClear(){
this.item = {};
this.dialogFormVisible = true
    },

    //新增学院
    saveDepartment() {
      
      org.addDepartment(this.item).then(response => {
        //添加成功
        //提示信息
        this.$message({
          type: "success",
          message: "添加成功!"
        });
        this.dialogFormVisible = false;
        this.getList();
      });
    },

    //查询单个学院详情
    getDepartmentByCode(departmentCode) {
      org
        .getDepartmentByCode(departmentCode)
        .then(response => {
          this.item = response.data.item;
          this.dialogFormVisible = true;
        }) //请求成功
        .catch(error => {
          console.log(error);
        }); //请求失败
    },

    //新增或修改学院信息
    saveOrUpdateDepartment() {
      if(!this.item.id){
        this.saveDepartment()
      }else{
        this.editDepartment();
      }
    },

    //修改学院信息
    editDepartment() {
      org
        .updateDepartment(this.item)
        .then(response => {
          this.dialogFormVisible = false;
          this.getList();
        }) //请求成功
        .catch(error => {
          console.log(error);
        });
    },
    //删除用户
    removeDataById(departmentCode) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        org
          .delDepartment(departmentCode)
          .then(response => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getList();
          }) //请求成功
          .catch(error => {
            console.log(error);
          });
      });
    }
  }
};
</script>